#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <jni.h>
#include "mchpaysdk.h"
#include "base64.h"
#include "md5.h"

MCHINI mchini;
void Java_com_mchsdk_paysdk_jni_MCHKeyTools_natInit(JNIEnv* env, jobject thiz) { //初始化
	/** 梦对称加密用的字符串*/
	mchini.MCHENCRYPTIONKEY = "gnauhcgnem";
}
//解密
char* mengchuang_decrypt(uint8* tempdata) {
	int i = strlen(tempdata);
	uint8 temppsd[20];
	int j = strlen(mchini.MCHENCRYPTIONKEY);
	int m = 0;
	uint8* outstr = malloc(i);
	int templen = 0;
	int start = 0;
	memset(outstr, 0, i);
	memset(temppsd, 0, 20);
	memcpy(temppsd, mchini.MCHENCRYPTIONKEY, j);
	templen = base64_decode((const uint8*) tempdata, strlen(tempdata),
			(uint8*) outstr);
	while (start < templen) {
		outstr[start] = outstr[start] ^ temppsd[m >= j ? m = 0 : m++];
		start++;
	}
	return (char*) outstr;
}
jstring //接收java参数
Java_com_mchsdk_paysdk_jni_MCHKeyTools_secToNor(JNIEnv* env, jobject thiz,
		jstring str) {
	char *strt;
	strt = (*env)->GetStringUTFChars(env, str, 0);
	char* tempchar = mengchuang_decrypt(strt);
	jstring outstr = (*env)->NewStringUTF(env, tempchar);
	free(tempchar);
	return outstr;
}
